import { Body, Controller, Delete, Get, Header, Param, ParseIntPipe, Patch, Post, Query, Request } from "@nestjs/common";
import { ApiBearerAuth, ApiBody, ApiOperation, ApiParam, ApiQuery, ApiResponse, ApiTags } from "@nestjs/swagger";
import { ClassService } from "./class.service";
import { CreateClassDto } from "./dto/create-class.dto";
import { UpdateClassDto } from "./dto/update-class.dto";
import type { QueryType } from "src/types/index.types";

@Controller("/school/class")
@ApiTags("班级相关")
export class ClassController {
  constructor(private readonly classService: ClassService) {}

  @Post()
  @ApiOperation({ summary: "新增班级", description: "新增班级-老师传入ID" })
  @ApiResponse({ status: 200, description: "success" })
  @ApiBody({ type: CreateClassDto })
  @ApiBearerAuth()
  create(@Body() createClassDto: CreateClassDto, @Request() request) {
    return this.classService.create(createClassDto, request);
  }

  @Get()
  @ApiOperation({ summary: "查询班级列表", description: "查询班级列表" })
  @ApiQuery({ name: "pageNum", required: false, description: "页码", type: Number })
  @ApiQuery({ name: "pageSize", required: false, description: "每页条数", type: Number })
  @ApiQuery({ name: "isAsc", required: false, description: "排序方式 desc-降序 asc-升序", enum: ["desc", "asc"] })
  @ApiQuery({ name: "teacherName", required: false, description: "老师姓名", type: String })
  @ApiResponse({ status: 200, description: "班级列表", type: CreateClassDto })
  @ApiBearerAuth()
  findAll(@Query() query: QueryType) {
    return this.classService.findAll(query);
  }

  @Get("/export") // 详细路由必须在动态路由上方 如: @Get(":id")
  @Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  @Header("Content-Disposition", "attachment; filename=filename.xlsx")
  @ApiOperation({ summary: "导出教师", description: "导出教师" })
  @ApiResponse({ status: 200, description: "success", type: Buffer })
  @ApiBearerAuth()
  export() {
    return this.classService.export();
  }

  @Get(":id")
  @ApiOperation({ summary: "获取班级详情", description: "获取班级详情" })
  @ApiResponse({ status: 200, description: "success" })
  @ApiParam({ name: "id", type: Number })
  @ApiBearerAuth()
  findOne(@Param("id", new ParseIntPipe()) id: number) {
    return this.classService.findOne(id);
  }

  @Patch(":id")
  @ApiOperation({ summary: "更新班级", description: "更新班级" })
  @ApiResponse({ status: 200, description: "success" })
  @ApiParam({ name: "id", type: Number })
  @ApiBody({ type: CreateClassDto })
  @ApiBearerAuth()
  update(@Param("id", new ParseIntPipe()) id: number, @Body() updateClassDto: UpdateClassDto, @Request() request) {
    return this.classService.update(id, updateClassDto, request);
  }

  @Delete(":id")
  @ApiOperation({ summary: "删除班级", description: "删除班级" })
  @ApiResponse({ status: 200, description: "success" })
  @ApiParam({ name: "id", type: Number })
  @ApiBearerAuth()
  remove(@Param("id", new ParseIntPipe()) id: number) {
    return this.classService.remove(id);
  }
}
